/*
    Copyright 2011 Peter Pratscher
 
    This file is part of Surveillance.NET.

    Surveillance.NET is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    Surveillance.NET is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Surveillance.NET. If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.Serialization;
using System.Threading;
using AForge.Video.VFW;

namespace SurveillanceDotNet.Model.AlarmActions
{
    [Serializable]
    public class RecordVideoAlarmAction : AlarmAction
    {
        public string DirectoryPath { get; set; }
        public int RecordingTime { get; set; }

        public RecordVideoAlarmAction(string directoryPath)
            : base()
        {
            DirectoryPath = directoryPath;
            if (!Directory.Exists(DirectoryPath)) Directory.CreateDirectory(DirectoryPath);

            RecordingTime = 10;
            Status = "Not active";
        }

        public override void Action()
        {
            Status = "Active";

            Thread.Sleep(RecordingTime);

            for (int i = 0; i < _Database.Cameras.Count; i++)
            {
                int cameraIndex = i;
                string fileName = Path.Combine(DirectoryPath, "camera_" + i + "_" + DateTime.Now.ToString("ddMMyyyy_hhmmss") + ".avi");

                if (File.Exists(fileName)) File.Delete(fileName);

                ThreadStart starter = new ThreadStart(delegate
                {
                    Record(_Database.Cameras[cameraIndex], fileName);
                });

                new Thread(starter).Start();
            }
        }

        private void Record(SurveillanceCamera camera, string fileName)
        {
            try
            {
                DateTime endTime = DateTime.Now.AddSeconds(RecordingTime);

                AVIWriter writer = new AVIWriter("DIB");
                writer.Open(fileName, camera.CurrentFrame.Width, camera.CurrentFrame.Height);

                while (DateTime.Now < endTime)
                {
                    writer.AddFrame((Bitmap)camera.CurrentFrame.Clone());
                    Thread.Sleep(500);
                }

                writer.Close();
                writer.Dispose();
            }
            catch (Exception ex)
            {
                //
            }

            Status = "Not active";
        }

        #region ISerializable Implementation
        protected RecordVideoAlarmAction(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
            DirectoryPath = info.GetString("DirectoryPath");
            RecordingTime = info.GetInt32("RecordingTime");
            Status = "Not active";
        }

        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            base.GetObjectData(info, context);
            info.AddValue("DirectoryPath", DirectoryPath);
            info.AddValue("RecordingTime", RecordingTime);
        }
        #endregion
    }
}
